package com.xinfu.newproject.ui.matter_repair;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.RepairEvaluateActivity;
import com.xinfu.newproject.activity.WebPayActivity;
import com.xinfu.newproject.adapter.matter_repair_adapter.RepairDetailAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.matter_repair.RepairDetailBean;
import com.xinfu.newproject.bean.matter_repair.RepairRecordBean;
import com.xinfu.newproject.bean.matter_repair.bean.RepairCountBean;
import com.xinfu.newproject.bean.pay.PayBean;
import com.xinfu.newproject.interface_.SwichFragmentInterface;
import com.xinfu.newproject.presenter.PayPresenter;
import com.xinfu.newproject.presenter.RepairRecordPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;
import com.xinfu.newproject.widget.picture.ViewPagerActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class RepairDetailsActivity extends BaseActivity implements IView.IRepairRecordView, IView.IPayView {

    @BindView(R.id.activity_repair_details_rv)
    RecyclerView mRecyclerView;

    private RepairDetailAdapter repairDetailAdapter;

    private SwichFragmentInterface swichFragmentInterface;

    private RepairRecordPresenter repairRecordPresenter = new RepairRecordPresenter(this);
    private PayPresenter payPresenter = new PayPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_details);
        ButterKnife.bind(this);
        registerBack();
        Bundle bundle = getIntent().getExtras();
        setActivityTitle(bundle.getString("title"));
        initViewBind();
        initLisener();
    }

    String repairId;

    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent().getExtras() != null) {
            if (getIntent().getExtras().getInt("type") == 0) {
                String response = getIntent().getExtras().getString("response");
                RepairRecordBean.ResultBean resultBean = new Gson().fromJson(response, RepairRecordBean.ResultBean.class);
                repairId = resultBean.getId();
                int ok = isLogin(0);
                if (ok == 1) {
                    repairRecordPresenter.repairDetail();
                }
            }
        }
    }

    private void initLisener() {

    }

    private void initViewBind() {
        setActivityTitle("报修详情");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        View headerView = getHeaderView();
        repairDetailAdapter = new RepairDetailAdapter();
        repairDetailAdapter.openLoadAnimation();
        repairDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        repairDetailAdapter.addHeaderView(headerView);
        mRecyclerView.setAdapter(repairDetailAdapter);
    }

    TextView payType1, payType2, noTv, statusTv;
    Button button, button2;

    private View getHeaderView() {
        View view = getLayoutInflater().inflate(R.layout.recyclerview_header_complaint_detail, (ViewGroup) mRecyclerView.getParent(), false);
            TextView titleTv = (TextView) view.findViewById(R.id.recycler_header_complaint_detail_title);
            TextView contentTv = (TextView) view.findViewById(R.id.recycler_header_complaint_detail_content);
        statusTv = (TextView) view.findViewById(R.id.header_repair_detail_pay_status);
            noTv = (TextView) view.findViewById(R.id.header_repair_detail_repair_no);
            TextView typeTv = (TextView) view.findViewById(R.id.header_repair_detail_pay_type);
            payType1 = (TextView) view.findViewById(R.id.header_repair_detail_pay_fee_of1);
            payType2 = (TextView) view.findViewById(R.id.header_repair_detail_pay_fee_of2);
            ImageView imageView1 = (ImageView) view.findViewById(R.id.recycler_header_complaint_detail_iv1);
            ImageView imageView2 = (ImageView) view.findViewById(R.id.recycler_header_complaint_detail_iv2);
            ImageView imageView3 = (ImageView) view.findViewById(R.id.recycler_header_complaint_detail_iv3);
            ImageView imageView4 = (ImageView) view.findViewById(R.id.recycler_header_complaint_detail_iv4);
            RelativeLayout relativeLayout = view.findViewById(R.id.header_repair_detail_pay_status_ll);
            LinearLayout linearLayout = view.findViewById(R.id.header_repair_detail_ll);
            button = view.findViewById(R.id.header_repair_detail_pay_status_btn);
            button2 = view.findViewById(R.id.header_repair_detail_evaluate);

            if (getIntent().getExtras() != null) {
                if (getIntent().getExtras().getInt("type") == 0) {
                    String response = getIntent().getExtras().getString("response");
                    RepairRecordBean.ResultBean resultBean = new Gson().fromJson(response, RepairRecordBean.ResultBean.class);
                    if (resultBean.getStatus() >= 600) {
                        if (resultBean.getConfirmPay() != null) {
                            relativeLayout.setVisibility(View.VISIBLE);
                            linearLayout.setVisibility(View.VISIBLE);
                            if (resultBean.getConfirmPay() == 0) {
                                typeTv.setText("支付方式: 线上支付");
                                if (resultBean.getIsPay() != null) {
                                    if (resultBean.getIsPay() == 0) {
                                        statusTv.setText("待支付");
                                        statusTv.setTextColor(getResources().getColor(R.color.green_37));
                                        button.setVisibility(View.VISIBLE);
                                        button.setText("去支付");
                                    } else if (resultBean.getIsPay() == 1) {
                                        statusTv.setText("已支付");
                                        statusTv.setTextColor(getResources().getColor(R.color.colorAccent));
                                        button.setVisibility(View.VISIBLE);
                                        button.setText("去评价");
                                    }
                                }
                            } else if (resultBean.getConfirmPay() == 1) {
                                typeTv.setText("支付方式: 线下支付");
                                statusTv.setText("已支付");
                                statusTv.setTextColor(getResources().getColor(R.color.colorAccent));
                                button.setVisibility(View.VISIBLE);
                                button.setText("去评价");
                            }
                        } else {
                            relativeLayout.setVisibility(View.GONE);
                            linearLayout.setVisibility(View.GONE);
                        }
                        if (resultBean.getStatus() == 900) {
                            button.setVisibility(View.GONE);
                        } else {
                            button.setVisibility(View.VISIBLE);
                        }

                        mobile = resultBean.getMobile();
                        roomId = resultBean.getRoomId();
                        repairNo = resultBean.getNo();

                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (resultBean.getIsPay() == 0) {
                                    if (statusTv.getText().toString().equals("已支付")) {
                                        if (repairId != null) {
                                            Bundle bundle = new Bundle();
                                            bundle.putString("repairId", repairId);
                                            startIntentActivity(RepairEvaluateActivity.class, bundle);
                                        }
                                    } else {
                                        if (amount != 0 && repairNo != null && roomId != null && mobile != null) {
                                            if (isLogin(0) == 1) {
                                                token = String.valueOf(resultBean.getTenantId());
                                                payPresenter.pay();
                                            }
                                        } else if (amount == 0) {
                                            BToast.showText(RepairDetailsActivity.this, "金额错误，请联系物业进行处理!");
                                        } else {
                                            BToast.showText(RepairDetailsActivity.this, "参数错误");
                                        }
                                    }

                                } else if (resultBean.getIsPay() == 1) {
                                    if (repairId != null) {
                                        Bundle bundle = new Bundle();
                                        bundle.putString("repairId", repairId);
                                        startIntentActivity(RepairEvaluateActivity.class, bundle);
                                    }
                                }
                            }
                        });
                        button2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (repairId != null) {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("repairId", repairId);
                                    startIntentActivity(RepairEvaluateActivity.class, bundle);
                                }
                            }
                        });
                        if (resultBean.getRepairArea() == 0) {
                            button2.setVisibility(View.GONE);
                            linearLayout.setVisibility(View.VISIBLE);
                        } else if (resultBean.getRepairArea() == 1) {
                            linearLayout.setVisibility(View.GONE);
                            if (resultBean.getStatus() == 900) {
                                button2.setVisibility(View.GONE);
                            } else {
                                button2.setVisibility(View.VISIBLE);
                            }
                        }
                    } else {
                        relativeLayout.setVisibility(View.GONE);
                        linearLayout.setVisibility(View.GONE);
                    }
                    if (resultBean.getRepairArea() == 0) {
                        titleTv.setText("个人住宅 - " + resultBean.getRepairPart());

                    } else if (resultBean.getRepairArea() == 1) {
                        titleTv.setText("公共区域 - " + resultBean.getRepairPart());

                    }
                    contentTv.setText(resultBean.getContents());
                    if (resultBean.getAttachFile() != null) {
                        if (resultBean.getAttachFile().length() > 5) {
                            String[] imageList = resultBean.getAttachFile().split(";");
                            switch (imageList.length) {
                                case 1:
                                    imageView1.setVisibility(View.VISIBLE);
                                    imageView2.setVisibility(View.GONE);
                                    imageView3.setVisibility(View.GONE);
                                    imageView4.setVisibility(View.GONE);
                                    Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                                    break;
                                case 2:
                                    imageView1.setVisibility(View.VISIBLE);
                                    imageView2.setVisibility(View.VISIBLE);
                                    imageView3.setVisibility(View.GONE);
                                    imageView4.setVisibility(View.GONE);
                                    Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                                    Glide.with(this).load(imageList[1]).asBitmap().error(R.mipmap.ic_launcher).into(imageView2);
                                    break;
                                case 3:
                                    imageView1.setVisibility(View.VISIBLE);
                                    imageView2.setVisibility(View.VISIBLE);
                                    imageView3.setVisibility(View.VISIBLE);
                                    imageView4.setVisibility(View.GONE);
                                    Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                                    Glide.with(this).load(imageList[1]).asBitmap().error(R.mipmap.ic_launcher).into(imageView2);
                                    Glide.with(this).load(imageList[2]).asBitmap().error(R.mipmap.ic_launcher).into(imageView3);
                                    break;
                                case 4:
                                    imageView1.setVisibility(View.VISIBLE);
                                    imageView2.setVisibility(View.VISIBLE);
                                    imageView3.setVisibility(View.VISIBLE);
                                    imageView4.setVisibility(View.VISIBLE);
                                    Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                                    Glide.with(this).load(imageList[1]).asBitmap().error(R.mipmap.ic_launcher).into(imageView2);
                                    Glide.with(this).load(imageList[2]).asBitmap().error(R.mipmap.ic_launcher).into(imageView3);
                                    Glide.with(this).load(imageList[3]).asBitmap().error(R.mipmap.ic_launcher).into(imageView4);
                                    break;
                            }
                        } else {
                            imageView1.setVisibility(View.GONE);
                            imageView2.setVisibility(View.GONE);
                            imageView3.setVisibility(View.GONE);
                            imageView4.setVisibility(View.GONE);
                        }
                    } else {
                        imageView1.setVisibility(View.GONE);
                        imageView2.setVisibility(View.GONE);
                        imageView3.setVisibility(View.GONE);
                        imageView4.setVisibility(View.GONE);
                    }
                    imageView1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("picture", resultBean.getAttachFile());
                            bundle1.putInt("position", 0);
                            startIntentActivityAnim(ViewPagerActivity.class, imageView1, bundle1);

                        }
                    });
                    imageView2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("picture", resultBean.getAttachFile());
                            bundle1.putInt("position", 1);
                            startIntentActivityAnim(ViewPagerActivity.class, imageView2, bundle1);
                        }
                    });
                    imageView3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("picture", resultBean.getAttachFile());
                            bundle1.putInt("position", 2);
                            startIntentActivityAnim(ViewPagerActivity.class, imageView3, bundle1);
                        }
                    });
                    imageView4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("picture", resultBean.getAttachFile());
                            bundle1.putInt("position", 3);
                            startIntentActivityAnim(ViewPagerActivity.class, imageView4, bundle1);
                        }
                    });

                }
            }
        return view;
    }


    @OnClick({R.id.head_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                finish();
                break;

        }
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private Integer isLogin(int type) {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            return 0;
        }


        return 1;
    }


    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String repairId() {
        return repairId;
    }

    @Override
    public String mobile() {
        return mobile;
    }

    @Override
    public String payType() {
        return "alipay";
    }

    @Override
    public String billType() {
        return "3";
    }

    @Override
    public String roomId() {
        return roomId;
    }

    @Override
    public String payFee() {
        return String.valueOf(amount);
    }

    @Override
    public String repairOrder() {
        return repairNo;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(this, content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
        showLoding();
    }

    @Override
    public void hideProgress() {
        hideLoding();
    }

    @Override
    public void getMsg(PayBean payBean) {
        if (payBean.getResult().getPay_url() != null) {
            Bundle bundle = new Bundle();
            bundle.putString("response", payBean.getResult().getPay_url());
            startIntentActivity(WebPayActivity.class, bundle);
        }
    }

    @Override
    public void getData(RepairRecordBean repairRecordBean) {

    }

    String mobile, roomId, repairNo;
    double amount;

    @Override
    public void getData(RepairDetailBean repairDetailBean) {
        List<RepairDetailBean.ResultBean.RepairStatusListBean> list = new ArrayList<>();
        if (repairDetailBean.getResult().getRepairStatusList().size() > 0) {
            for (int i = 0; i < repairDetailBean.getResult().getRepairStatusList().size(); i++) {
                if (i == 0) {
                    repairDetailBean.getResult().getRepairStatusList().get(0).setPosition(0);
                } else {
                    repairDetailBean.getResult().getRepairStatusList().get(i).setPosition(1);
                }
                list.add(repairDetailBean.getResult().getRepairStatusList().get(i));
            }
        }
        repairDetailAdapter.detail(repairDetailBean.getResult().getUserName(), repairDetailBean.getResult().getCreateTime());
        repairDetailAdapter.setNewData(list);
        if (repairDetailBean.getResult().getCharge() != null) {
            repairNo = repairDetailBean.getResult().getCharge().getRepairNo();
            mobile = repairDetailBean.getResult().getMobile();
            roomId = repairDetailBean.getResult().getRoomId();
            amount = repairDetailBean.getResult().getCharge().getRepairManHourfee() + repairDetailBean.getResult().getCharge().getRepairMaterialFee();
            if (payType1 != null && payType2 != null) {
                payType1.setText("工时费：￥" + String.valueOf(repairDetailBean.getResult().getCharge().getRepairManHourfee()));
                payType2.setText("物料费：￥" + String.valueOf(repairDetailBean.getResult().getCharge().getRepairMaterialFee()));
            }
            if (repairDetailBean.getResult().getCharge().getRepairNo() != null) {
                noTv.setText("支付订单: " + repairDetailBean.getResult().getCharge().getRepairNo());
            }
        }
        if (repairDetailBean.getResult().getRepairStatusList().size() > 0) {
            if (repairDetailBean.getResult().getRepairStatusList().get(0).getStatus() == 900) {
                if (button != null) {
                    button.setVisibility(View.GONE);
                }
                if (button2 != null) {
                    button2.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public void getData(RepairCountBean repairCountBean) {

    }
}